#include "KeyboardLayer.h"

bool KeyboardLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}

	m_listen = true;
	m_isToning = false;
	m_count = 0;

	m_lastCardNumber = "";
	m_selfWait = false;

	m_busy = false;
	m_receiveBuffer = NULL;

	this->setKeypadEnabled(true);
	this->setKeyboardEnabled(true);
	auto keyboardListener = EventListenerKeyboard::create();
	keyboardListener->onKeyReleased = CC_CALLBACK_2(KeyboardLayer::keyReleased, this);

	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this);


	return true;
}

KeyboardLayer::~KeyboardLayer()
{
	if (m_receiveBuffer != NULL)
	{
		delete[] m_receiveBuffer;
	}
}

void KeyboardLayer::keyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
{
	if (m_count == 0)
	{
		if (m_receiveBuffer != NULL) 
		{
			delete[] m_receiveBuffer;
		}
		m_receiveBuffer = new BYTE[255];
		memset(m_receiveBuffer, 0, 255);
	}

	char tempChar = 0x00;
	char hh[10] = {};
	sprintf(hh, "%d", keyCode);
	int code;
	code = atoi(hh);
	if (code > 75 && code < 86)
	{
		tempChar = (BYTE)'0' + (code - 76);
	}
	else if (code > 123 && code < 150)
	{
		tempChar = (BYTE)'a' + (code - 124);
	}

	if (tempChar != 0x00)
	{
		m_receiveBuffer[m_count++] = tempChar;
	}

	keyCodeHandle(keyCode);

	if (m_count == 0)
	{
		m_busy = true;
		scheduleOnce(schedule_selector(KeyboardLayer::clearBusy),1.0f);
		//MessageBox(m_cardNumber.c_str(), "card scan");
	}
}

void KeyboardLayer::clearBusy(float dt)
{
	m_busy = false;
}

void KeyboardLayer::keyCodeHandle(cocos2d::EventKeyboard::KeyCode keyCode)
{
	if (keyCode == EventKeyboard::KeyCode::KEY_ENTER)
	{
		if (m_busy)
			return;

		if (!m_listen)
		{
			tone(false);
			m_count = 0;
			return;
		}

		stringstream stream;
		stream << m_receiveBuffer;
		m_cardNumber = stream.str().c_str();
		for (int i = 0; i < m_cardNumber.length(); i++)
		{
			if (m_cardNumber[i] >= 97 && m_cardNumber[i] <= 122)
			{
				m_cardNumber[i] = m_cardNumber[i] - 32;
			}
		}
		//stringstream stream;
		//for (int i = 4; i > 0; i--)
		//{
		//	int index = i * 2;
		//	stream << m_receiveBuffer[index];
		//	stream << m_receiveBuffer[index + 1];
		//}
		//m_cardNumber = String::createWithFormat("%d", FormatUtil::hexToDecimal(stream.str().c_str(), 8))->getCString();
		m_count = 0;

		bool bTone = true;
		if (m_selfWait)
		{
			if (m_lastCardNumber.size() == 0 || 0 == strcmp(m_lastCardNumber.c_str(), m_cardNumber.c_str())) 
			{
				m_lastCardNumber = m_cardNumber;
			}
			else
			{
				bTone = false;
				m_cardNumber = "";
			}
			
		}

		tone(bTone);
	}
}

void KeyboardLayer::tone(bool success)
{
	if (m_isToning)
		return;

	m_isToning = true;

	int audioID = -1;
	if (success)
	{
		audioID = AudioEngine::play2d("tsound/keyboard_success.mp3");
	}
	else
	{
		audioID = AudioEngine::play2d("tsound/keyboard_warn.mp3");
	}
	if (audioID != -1)
	{
		AudioEngine::setFinishCallback(audioID, CC_CALLBACK_0(KeyboardLayer::onToneComplete, this));
	}
	else
	{
		m_isToning = false;
	}
}

void KeyboardLayer::onToneComplete()
{
	m_isToning = false;
}